<?php
class myDB{
	
	
	private $__numrows;
	public $__connection;
	public $__selected_db;
	
	private $__last_query_duration = 0;
	
	public $__log;
	
	private $__executed_queries;
	
	public function __construct($host,$username,$password,$database){
		
		$this->__connection = mysql_connect($host,$username,$password);
		
		if(!$this->__connection){
			
			throw new ErrorException(mysql_error());
			
		}
		else{
			
			$this->__selected_db = mysql_select_db($database);
			
			if(!$this->__selected_db){
				
				
				throw  new ErrorException(mysql_error());
				
			}
			
			
		}
	}
	
	public function selected_db($database){
		
		
		$this->__selected_db = mysql_selected_db($database);
		
		if(!$this->__selected_db){
			
			throw new ErrorException(mysql_error());	
			
		}
		
	}
	
	
	
	
	public function queryOne($str){
		
		
		$startTime = microtime();
		$sql = mysql_query($str);
		
		$this->log($startTime,$str,@mysql_num_rows($sql),0,0,mysql_error());
		$result = mysql_fetch_assoc($sql);
		if(mysql_num_rows($sql)>0){
			
			return $result;
			
		}
		else{
			
			return false;
			
		}
		
	}
	
	public function query($str){
		
		$startTime = microtime();
		$sql = mysql_query($str,$this->__connection);
		$this->__last_query_duration = self::execTime($startTime);
		$rs["numrows"] = @mysql_num_rows($sql);
		$this->log($startTime,$str,$rs["numrows"],0,0,mysql_error());
		
		if ($rs["numrows"]>0){
		
			while($resultset = mysql_fetch_array($sql)):

        		while(list($arrayKey,$arrayValue)=each($resultset)):

        			if(!is_numeric($arrayKey)){
            	
						$rs[$arrayKey][] = $arrayValue;

          			}
				
				endwhile;

     		 endwhile;			
		
		}
		
		return $rs;
	}
	
	public function insert($str){
		
		$startTime = microtime();
		$sql = mysql_query($str);
		$inserted_id = mysql_insert_id();
		$this->log($startTime,$str,0,0,$inserted_id,mysql_error());
		
		if(mysql_error()){
			
			return false;
			
			
		}
		else{
			
			if(!empty($inserted_id)){
			
				return $inserted_id;
			
			}
			else{
			
				return false;
			
			}
		
		}
		
	}
	
	private function log($startTime,$queryString,$numrows,$affected_rows,$inserted_id,$errorMsg){
		
		$this->__last_query_duration = self::execTime($startTime);
		$this->__log["duration"][] = $this->__last_query_duration;
		$this->__log["queryString"][] = $queryString;
		$this->__log["numrows"][] = $numrows;
		$this->__log["affected_rows"][] = $affected_rows;
		$this->__log["inserted_id"][] = $inserted_id;
		$this->__log["errorMsg"][] = $errorMsg;
	}
	public function update($str){
		
		$startTime =microtime();
		$sql = mysql_query($str,$this->__connection);
		$affected_rows = @mysql_affected_rows($sql);
		if($affected_rows>0){
			
			return $affected_rows;
			
		}
		else{
			
			return false;
			
		}
		
		$this->log($startTime,$str,0,$affected_rows,"",mysql_error());
		
	}
	
	public function displayLog($cssClass=""){
		
		$header = "<table class=\"".$cssClass."\">
		<tr>
		<th>Query String</th>
		<th>Number of Rows</th>
		<th>Affected Rows</th>
		<th>Inserted ID</th>
		<th>Error Message</th>
		<th>Duration</th>
		</tr>";
		
		$rows = count($this->__log["queryString"]);
		$content = "";
		for($i=0;$i<$rows;$i++){
			
			$content .= "<tr>";
			$content .= "<td>".$this->__log["queryString"][$i]."</td>";
			$content .= "<td>".$this->__log["numrows"][$i]."</td>";
			$content .= "<td>".$this->__log["affected_rows"][$i]."</td>";
			$content .= "<td>".$this->__log["inserted_id"][$i]."</td>";
			$content .= "<td>".$this->__log["errorMsg"][$i]."</td>";
			$content .= "<td>".$this->__log["duration"][$i]."ms</td>";
			$content .= "</tr>";
			
		}
		
		return $header.$content."</table>";
		
	}
	
	public function delete($str){
		$startTime = microtime();
		echo $str;
		$str = mysql_escape_string($str);
		echo "<hr>".$str;
		exit;
		$sql = mysql_query($str,$this->__connection);
		
		$affected_rows = @mysql_affected_rows($sql);
		if($affected_rows>0){
			
			return $affected_rows;
			
		}
		else{
			
			return false;
			
		}
		
		$this->log($startTime,$str,0,$affected_rows,"",mysql_error());
	}
	
	function execTime($startTime){

	$stopTime = microtime();

	list ($start_ms,$start_sek) = split ('[ ]',$startTime);
	$startTime=$start_sek+$start_ms;
	$start_ms=eregi_replace("0.","",$start_ms);

	list ($stop_ms,$stop_sek) = split ('[ ]',$stopTime);
	$stopTime=$stop_sek+$stop_ms;
	$stop_ms=eregi_replace("0.","",$stop_ms);


	$runtime=$stopTime-$startTime;
	$runtime=sprintf ("%.3f", $runtime);
	return ($runtime*1000);

}  
	
	
}


?>